//
//  MainScreen.m
//  Opper
//
//  Created by Le Dung on 4/13/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "MainScreen.h"

@interface MainScreen ()

@end

@implementation MainScreen

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [StyleClass createNavi:self andTitle:@"Opper"];
    [StyleClass background:self.view];
    [self isFirstUser];
    [self buttonTarget];
    self.memberTable.backgroundColor=[UIColor clearColor];
    self.memberTable.separatorColor = [UIColor clearColor];
}

-(void)opperPress
{
    Document *page =[[[Document alloc]init] autorelease];
    page.isInstane =0;
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [GetData App].profileArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"MemberCell";
    MemberCell *cell = (MemberCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObj =  [[NSBundle mainBundle] loadNibNamed:@"MemberCell" owner:self options:nil];
        for (id currentObj in topLevelObj)
        {
            if ([currentObj isKindOfClass:[UITableViewCell class]])
            {
                cell = (MemberCell*)currentObj;
                break;
            }
        }
        
        Member *member =[[Member alloc]init];
        member =[GetData App].profileArray[indexPath.row];
        
        [cell.unitMember setTitle:[member.firstName stringByAppendingString:[NSString stringWithFormat:@" %@",member.lastname]] forState:UIControlStateNormal];
        cell.unitMember.tag =indexPath.row;
        [cell.unitMember setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
        
    }
    [cell.unitMember addTarget:self action:@selector(memberDetail:) forControlEvents:UIControlEventTouchUpInside ];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)memberDetail:(UIButton*)button
{
    ContractList *page =[[ContractList alloc]init] ;
    page.member=[GetData App].profileArray[button.tag];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}



-(void)buttonTarget
{
    [self.addButton addTarget:self action:@selector(gotoProfile) forControlEvents:UIControlEventTouchUpInside];
    [self.opperButton setBackgroundImage:[StyleClass nGreen] forState:UIControlStateNormal];
    
    [self.opperButton addTarget:self action:@selector(opperPress) forControlEvents:UIControlEventTouchUpInside];
    
    [self.fqaButton addTarget:self action:@selector(fqaPress) forControlEvents:UIControlEventTouchUpInside];
    [self.fqaButton setBackgroundImage:[StyleClass nGray] forState:UIControlStateNormal];

    
    [self.aboutButton addTarget:self action:@selector(aboutPress) forControlEvents:UIControlEventTouchUpInside];
    [self.aboutButton setBackgroundImage:[StyleClass nGray] forState:UIControlStateNormal];

}

-(void)fqaPress
{
}
-(void)aboutPress
{
    
}
-(void)gotoProfile
{
    Profile *page =[[[Profile alloc]init]autorelease];
    [StyleClass currentScreen:self andDestination:page andHideBackButton:NO];
    
}


-(void)isFirstUser
{
    NSUserDefaults  *userDefault =[NSUserDefaults standardUserDefaults];
    if(![userDefault objectForKey:@"isFirstUser"])
    {
        [userDefault setObject:[NSNumber numberWithBool:YES] forKey:@"isFirstUser"];
        [userDefault synchronize];
        
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"U hebt nog geen profiel aangemaakt. Klik op de + button om een profiel te maken alvorens verder te gaan." message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil ];
        [alert show];
        [alert release];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_opperButton release];
    [_fqaButton release];
    [_aboutButton release];
    [_addButton release];
    [_memberTable release];
    [super dealloc];
}
@end
